﻿using System;
using System.Web.UI;
using System.IO;
using Yule8xCommon.Applib;

namespace Yule8xWechat.Other
{
    public partial class DebugTool : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (File.Exists(RuntimeDataUtil.filePathes.onecentusers_file))
                {
                    tb_onecentusers.Text = File.ReadAllText(RuntimeDataUtil.filePathes.onecentusers_file);
                }
                // 是否调试模式
                chkDebugMode.Checked = RuntimeDataUtil.CheckDebugModel();
                // 是否禁止更新游戏库
                chkForbidUpdateGameDb.Checked = RuntimeDataUtil.CheckForbidUpdateGameDb();
                // 用户关注微信欢迎语
                tb_welcomestr.Text = RuntimeDataUtil.GetWelcomeText();
                // 微信公众号菜单
                tb_menu.Text = RuntimeDataUtil.GetMenuText(Reslib.Menu);
                // 微信公众号关键词
                tb_keywords.Text = RuntimeDataUtil.GetKeywordText(string.Format("开单888888:https://wx.34537.com/XAgent/Index.html{0}测起来666:http://wxcs.34537.com/Test/Default.aspx", Environment.NewLine));
                // 超级验证码
                tb_captcha.Text = RuntimeDataUtil.GetCaptchaText("9909");
            }
        }

        protected void btn_save_onecentusers_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(RuntimeDataUtil.filePathes.onecentusers_file))
            {
                File.WriteAllText(RuntimeDataUtil.filePathes.onecentusers_file, tb_onecentusers.Text.Trim());
                Response.Redirect("DebugTool.aspx?result=success");
            }
            else
            {
                Response.Redirect("DebugTool.aspx?result=file_not_found");
            }
        }

        protected void btn_save_debugmode_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(RuntimeDataUtil.filePathes.debugmode_file))
            {
                string content = AppInfo.EncryptKey + DateTime.Today.ToString("yyyyMMdd") + chkDebugMode.Checked.ToString();
                File.WriteAllText(RuntimeDataUtil.filePathes.debugmode_file, MD5Util.GetShortMD5(content));
                File.WriteAllText(RuntimeDataUtil.filePathes.forbidupdategamedb_file, chkForbidUpdateGameDb.Checked.ToString());
                Response.Redirect("DebugTool.aspx?result=success");
            }
            else
            {
                Response.Redirect("DebugTool.aspx?result=file_not_found");
            }
        }

        protected void btn_save_welcomestr_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(RuntimeDataUtil.filePathes.welcomestr_file))
            {
                File.WriteAllText(RuntimeDataUtil.filePathes.welcomestr_file, tb_welcomestr.Text.Trim());
                Response.Redirect("DebugTool.aspx?result=success");
            }
            else
            {
                Response.Redirect("DebugTool.aspx?result=file_not_found");
            }
        }

        protected void btn_save_menu_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(RuntimeDataUtil.filePathes.menu_file))
            {
                File.WriteAllText(RuntimeDataUtil.filePathes.menu_file, tb_menu.Text.Trim());
                Response.Redirect("DebugTool.aspx?result=success");
            }
            else
            {
                Response.Redirect("DebugTool.aspx?result=file_not_found");
            }
        }

        protected void btn_init_menu_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(RuntimeDataUtil.filePathes.menu_file))
            {
                File.Delete(RuntimeDataUtil.filePathes.menu_file);
                Response.Redirect("DebugTool.aspx?result=success");
            }
            else
            {
                Response.Redirect("DebugTool.aspx?result=file_not_found");
            }
        }
        protected void btn_save_qrsence_Click(object sender, EventArgs e)
        {
            Response.Redirect("DebugTool.aspx?qrsence=" + tb_qrsence.Text.Trim());
        }

        protected void btn_save_keywords_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(RuntimeDataUtil.filePathes.keyword_file))
            {
                File.WriteAllText(RuntimeDataUtil.filePathes.keyword_file, tb_keywords.Text.Trim());
                Response.Redirect("DebugTool.aspx?result=success");
            }
            else
            {
                Response.Redirect("DebugTool.aspx?result=file_not_found");
            }
        }

        protected void btn_save_captcha_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(RuntimeDataUtil.filePathes.captcha_file))
            {
                File.WriteAllText(RuntimeDataUtil.filePathes.captcha_file, tb_captcha.Text.Trim());
                Response.Redirect("DebugTool.aspx?result=success");
            }
            else
            {
                Response.Redirect("DebugTool.aspx?result=file_not_found");
            }
        }
    }
}